// pintu.cpp: 定义控制台应用程序的入口点。
//
//初始界面头文件

#include"stdafx.h"
#include "stdafx.h"

#include <iostream>

#include <easyx.h>

#include <graphics.h>

#include <conio.h>

#include <time.h>



using namespace std;

const int width = 800;
const int height = 600;
const int row = 20;
const int column = 20;
const int maxtime = 600;

void Init();
void ShutDown();
void Logic();
void Draw();
void DrawOrigin();
int DrawTime();
void Devide(IMAGE*image);
int* Disorganize();
int GetIndex(MOUSEMSG mmsg);
void DrawSelect(int index);
void Exchange(int x, int y);
bool Check();
void GameOver(bool win);

IMAGE image;
IMAGE *images;

int *index;

unsigned long starttime = 0;

int select_index = 0;
bool isselect_index = false;
int resttime = maxtime;

int main()
{
	Init();
	Draw();
	Logic();
	ShutDown();
	return 0;
}

void Init()
{
	initgraph(width, height, SHOWCONSOLE);
	index = Disorganize();
	loadimage(&image, L"image.jpg", height, height, true);
	images = new IMAGE[row*column];
	Devide(&image);
	loadimage(&image, L"image.jpg", width - height, width - height, true);
	starttime = (unsigned long)time(NULL);
}

void ShutDown()
{
	delete[] images;
	closegraph();
}

void Logic()
{
	while (true)
	{
		Draw();
		bool win = Check();
		if (win)
		{
			GameOver(win);
		}
		if (resttime <= 0)
		{
			GameOver(false);
		}
		MOUSEMSG mmsg;
		if (MouseHit())
		{
			mmsg = GetMouseMsg();
			switch (mmsg.uMsg)
			{
			case WM_LBUTTONDOWN:
			{
				int _select_index = GetIndex(mmsg);
				if (select_index == _select_index)
				{
					if (isselect_index == false)
						isselect_index = true;
					else
						isselect_index = false;
				}
				else
				{
					if (!isselect_index)
					{
						select_index = _select_index;
						isselect_index = true;
					}
					else
					{
						Exchange(select_index, _select_index);
						isselect_index = false;
					}
				}
			}
			break;
			default:
				break;
			}
		}
	}
}

void Devide(IMAGE *image)
{
	SetWorkingImage(image);
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			TCHAR bf[10];
			wsprintf(bf, _T("%d"), i*row + j);
			lstrcat(bf, _T(".jpg"));
			getimage(images + i*row + j, (height / column)*j, (height / row)*i, height / column, height / row);
		}
	}
	SetWorkingImage(NULL);
}

int* Disorganize()
{
	index = new int[row*column];
	for (int i = 0; i < row*column; i++)
	{
		*(index + i) = -1;
	}
	for (int i = 0; i < column*row; i++)
	{
		bool cf = false;
		int k = 0;
		do
		{
			k++;
			cf = false;
			srand((unsigned long)time(NULL) + 3 * i + 3 * k);
			int t = rand() % (column*row);
			for (int j = 0; j < i; j++)
			{
				if (t == *(index + j))
				{
					cf = true;
				}
			}
			if (cf == false)
			{
				*(index + i) = t;
			}
		} while (cf == true);
	}
	return index;
}

void Draw()
{
	BeginBatchDraw();
	setbkcolor(BLACK);
	cleardevice();
	for (int i = 0; i< row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			putimage((height / column)*j, (height / row)*i, images + *(index + i*row + j));
		}
	}
	DrawOrigin();
	resttime = DrawTime();
	DrawSelect(select_index);
	EndBatchDraw();
}

void DrawOrigin()
{
	putimage(height, (height / 2 - (width - height) / 2), width - height, width - height, &image, 0, 0);
}

int DrawTime()
{
	RECT r = { height,height / 2 - (width - height) / 2 - 50,width,height / 2 - (width - height) / 2 };
	unsigned long nowtime = (unsigned long)time(NULL) - starttime;
	nowtime = maxtime - nowtime;
	int min = nowtime / 60;
	int sec = nowtime % 60;
	TCHAR bf[50] = _T("剩余时间:");
	TCHAR tbf[50];
	wsprintf(tbf, _T("%d"), min);
	lstrcat(bf, tbf);
	lstrcat(bf, _T(":"));
	wsprintf(tbf, _T("%d"), sec);
	lstrcat(bf, tbf);
	drawtext(bf, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	return nowtime;
}

void DrawSelect(int index)
{
	if (isselect_index == false)
		return;
	int x = index%row;
	int y = index / row;
	x = x*(height / row);
	y = y*(height / row);
	COLORREF col;
	col = getlinecolor();
	setlinecolor(CYAN);
	for (int i = 0; i < 3; i++)
	{
		rectangle(x - i, y - i, x + (height / row) - i, y + (height / row) - i);
	}
	setlinecolor(col);

}

int GetIndex(MOUSEMSG mmsg)
{
	int x = mmsg.x / (height / row);
	int y = mmsg.y / (height / row);
	return y*row + x;
}

void Exchange(int x, int y)
{
	int temp = index[x];
	index[x] = index[y];
	index[y] = temp;
}

bool Check()
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			if (*(index + i*row + j) != (i*row + j))
				return false;
		}
	}
	return true;
}

void GameOver(bool win)
{
	if (win)
	{
		setbkcolor(BLACK);
		cleardevice();
		LOGFONT f;
		gettextstyle(&f);
		f.lfWidth = 40;
		f.lfHeight = 40;
		settextstyle(&f);
		RECT r = { 0,0,width,height };
		drawtext(_T("You Win"), &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
		_getch();
	}
	else
	{
		setbkcolor(BLACK);
		cleardevice();
		LOGFONT f;
		gettextstyle(&f);
		f.lfWidth = 40;
		f.lfHeight = 40;
		settextstyle(&f);
		RECT r = { 0,0,width,height };
		drawtext(_T("You Lost"), &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
		_getch();
	}
}